package jdk8.stream;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.ToDoubleFunction;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamDemo {

	public static void main(String[] args) {
		//
		List<Order> ordList = getOrderList();
		//1. 统计未发货的订单数
		long count = ordList.stream().filter((o) -> o.getStatus().equals(OrderStatus.未发货)).count();
		System.out.println("未发货的订单数："+count);
		
		//2. 除已取消状态的所有订单 的费用总和
		Double value = ordList.parallelStream()
								//过滤
								.filter( (o) -> !o.getStatus().equals(OrderStatus.已取消))
								//归集 [可以做 求和，求平均值，求最大，最小，转换成 集合]
								.collect(Collectors.summarizingDouble((o) -> o.getCost())).getSum();
		System.out.println(value);	
		
		//3. 查询出费用大于100元的订单
		List<Order> resultList = ordList.parallelStream().filter((o) -> o.getCost() > 100)
								//归集操作 
								.collect(Collectors.toList());
		//
		resultList.forEach(System.out::println);
		//4. 统计出每种订单状态下的订单，以Map来存储结果, Map<String, List<Order>>
		Map<String, List<Order>> map = ordList.stream()
										.collect(Collectors.groupingBy( 
														(o) -> o.getStatus().toString(),
														Collectors.toList()
													)
												);
		//输出				
		map.forEach((k, v) -> {
			System.out.printf("状态为：%s 的订单有：\n",k);
			v.forEach(System.out::println);
		});
		
		
		System.out.println("===================================================");
		String[] arr = {"i have a dream","you are awesome!","no, how dare you"};
		//
		Arrays.stream(arr).map( (t) -> t.split(" ")).forEach(System.out::println);
		//使用flatMap
		Arrays.stream(arr).map( (t) -> t.split(" "))
					//.flatMap( (strArr) -> Arrays.stream(strArr)).forEach(System.out::println);
					.flatMap( Arrays::stream).forEach(System.out::println);
		
		//
		System.out.println("===========================================");
		Stream.iterate(1, x -> ++x).skip(5).limit(10).forEach(System.out::println);
		Stream.generate(Math::random).limit(10).forEach(System.out::println);
		
	}	

	private static List<Order> getOrderList() {
		Order o1 = new Order("1001", 45.6, OrderStatus.已付款, LocalDateTime.of(2020, 8, 13, 12, 15,38));
		Order o2 = new Order("1002", 145.6, OrderStatus.已付款, LocalDateTime.of(2020, 8, 18, 12, 15,38));
		Order o3 = new Order("1003", 445.6, OrderStatus.已付款, LocalDateTime.of(2020, 8, 30, 12, 15,38));
		Order o4 = new Order("1004", 15.6, OrderStatus.已发货, LocalDateTime.of(2020, 8, 29, 12, 15,38));
		Order o5 = new Order("1005", 67.6, OrderStatus.已发货, LocalDateTime.of(2020, 8, 27, 12, 15,38));
		Order o6 = new Order("1006", 119.6, OrderStatus.已取消, LocalDateTime.of(2020, 8, 20, 12, 15,38));
		Order o7 = new Order("1007", 28.6, OrderStatus.已签收, LocalDateTime.of(2020, 8, 18, 12, 15,38));
		Order o8 = new Order("1008", 345.6, OrderStatus.已签收, LocalDateTime.of(2020, 8, 12, 12, 15,38));
		Order o9 = new Order("1009", 1405.6, OrderStatus.未发货, LocalDateTime.of(2020, 9, 2, 18, 15,38));
		Order o10 = new Order("1010", 245.6, OrderStatus.未发货, LocalDateTime.of(2020, 9, 2, 17, 15,38));
		return Arrays.asList(o1,o2,o3,o4,o5,o6,o7,o8,o9,o10);
	}
	
	
}
